﻿using Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class HocVienProvider
    {
        public static List<HocVien> LayDanhSachHocVien()
        {
            List<HocVien> lst = new List<HocVien>();
            using(QLTTTHEntities context = new QLTTTHEntities())
            {
                var query = (from u in context.HocViens select u);

                    foreach(var row in query)
                    {
                        HocVien hv = new HocVien();
                        hv.MaHV = row.MaHV;
                        hv.HoTen = row.HoTen;
                        hv.GioiTinh = row.GioiTinh;
                        hv.NgaySinh = row.NgaySinh;
                        hv.DiaChi = row.DiaChi;
                        hv.Email = row.Email;
                        hv.SoDT = row.SoDT;
                        hv.NgheNghiep = row.NgheNghiep;
                        lst.Add(hv);
                    }
            }
            return lst;
        }

        public static List<HocVien> TimDanhSachHocVien(object objA, SoSanh ss)
        {
            var lst = new List<HocVien>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.HocViens select u);

                foreach (var row in query)
                {
                    var hv = new HocVien();
                    hv.MaHV = row.MaHV;
                    hv.HoTen = row.HoTen;
                    hv.GioiTinh = row.GioiTinh;
                    hv.NgaySinh = row.NgaySinh;
                    hv.DiaChi = row.DiaChi;
                    hv.Email = row.Email;
                    hv.SoDT = row.SoDT;
                    hv.NgheNghiep = row.NgheNghiep;

                    if (ss(objA, hv) == 0)
                    {
                        lst.Add(hv);
                    }
                }
            }

            return lst;
        }

        public static bool XoaHocVien(int maHV)
        {
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var query = (from u in context.HocViens
                                 where u.MaHV.Equals(maHV)
                                 select u).First();
                    context.HocViens.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatHocVien(HocVien hv)
        {
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    var query = (from u in context.HocViens
                                 where u.MaHV.Equals(hv.MaHV)
                                 select u).First();
                    query.HoTen = hv.HoTen;
                    query.GioiTinh = hv.GioiTinh;
                    query.NgaySinh = hv.NgaySinh;
                    query.DiaChi = hv.DiaChi;
                    query.Email = hv.Email;
                    query.SoDT = hv.SoDT;
                    query.NgheNghiep = hv.NgheNghiep;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static int ThemHocVien(HocVien hv)
        {
            int maHV = -1;
            try
            {
                using (QLTTTHEntities context = new QLTTTHEntities())
                {
                    context.HocViens.Add(hv);
                    context.SaveChanges();
                    maHV = hv.MaHV;
                }
            }
            catch
            {

            }
            return maHV;
        }
    }
}
